﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Web.Script.Serialization;
using DVK.SP2007.UniversalClient;

namespace DVK.SP2007
{
    /// <summary>
    /// Encapsulates a DvkElement for mapping between SharePoint field and Dvk Xml element. 
    /// <remarks>
    /// The class is serialized to JSON and deserialized from JSON. 
    /// It is used in the DvkSettings list to configure the Dvk elements that can be mapped with the SharePoint field.
    /// 
    /// JSON:
    /// {"Name":"", "Namespace":"", "Parent":"", "ParentNamespace:""}
    /// </remarks>
    /// </summary>
    public class DvkElement
    {
        /// <summary>
        /// Gets or sets the name of the element.
        /// </summary>
        /// <value>The name.</value>
        public string Name { get; set; }
        /// <summary>
        /// Gets or sets the namespace of the element.
        /// </summary>
        /// <value>The namespace.</value>
        public string Namespace { get; set; }

        /// <summary>
        /// Gets or sets the parent element name.
        /// </summary>
        /// <value>The parent.</value>
        public string Parent { get; set; }
        
        private string _parentNamespace;
        /// <summary>
        /// Gets or sets the parent elements namespace.
        /// <remarks>
        /// Default namespace is Dvk root namespace. 
        /// </remarks>
        /// </summary>
        /// <value>The parent namespace.</value>
        public string ParentNamespace 
        {
            get {
                if (string.IsNullOrEmpty(_parentNamespace))
                    return DvkXml.RootNamespace;
                return _parentNamespace;
            }
            set {
                _parentNamespace = value;
            }
        }

        /// <summary>
        /// Gets the element.
        /// </summary>
        /// <returns></returns>
        public XElement Get()
        {
            if (string.IsNullOrEmpty(Name) || string.IsNullOrEmpty(Namespace))
                return null;
            
            return new XElement(XName.Get(Name, Namespace));
        }

        /// <summary>
        /// Gets the parent element.
        /// </summary>
        /// <returns></returns>
        public XElement GetParent()
        {
            if (string.IsNullOrEmpty(Parent) || string.IsNullOrEmpty(ParentNamespace))
                return null;

            return new XElement(XName.Get(Parent, ParentNamespace));
        }
        /// <summary>
        /// Gets the element name with namespace.
        /// </summary>
        /// <value>The name with namespace.</value>
        [ScriptIgnore]
        public string NameWithNamespace
        {
            get
            {
                return Get().Name.ToString();
            }
        }
        /// <summary>
        /// Gets the parent element name with namespace.
        /// </summary>
        /// <value>The parent name with namespace.</value>
        [ScriptIgnore]
        public string ParentNameWithNamespace
        {
            get
            {
                return GetParent().Name.ToString();
            }
        }
        /// <summary>
        /// Gets the selection value.
        /// <remarks>
        /// The SelectionValue is the DvkElement in the form to be used in dropdownlists.
        /// Form: 
        ///     "NameWithNamespace;ParentNameWithNamespace"
        /// </remarks>
        /// </summary>
        /// <value>The selection value.</value>
        [ScriptIgnore]
        public string SelectionValue
        {
            get
            {
                return NameWithNamespace + ";" + ParentNameWithNamespace;
            }
        }
    }
}
